package org.jsola.hr.common;

import java.time.LocalDate;

public class IdcardUtils {

    /**
     * 正则校验是否是身份证号
     * @param IDNumber
     * @return
     */
    public static boolean isIDNumber(String IDNumber) {
        if (IDNumber == null || "".equals(IDNumber)) {
            return false;
        }
        // 定义判别用户身份证号的正则表达式（15位或者18位，最后一位可以为字母）
        String regularExpression =
                "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|"
                        + "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
        boolean matches = IDNumber.matches(regularExpression);
        // 判断第18位校验值
        if (matches) {
            if (IDNumber.length() == 18) {
                try {
                    char[] charArray = IDNumber.toCharArray();
                    // 前十七位加权因子
                    int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                    // 这是除以11后，可能产生的11位余数对应的验证码
                    String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                    int sum = 0;
                    for (int i = 0; i < idCardWi.length; i++) {
                        int current = Integer.parseInt(String.valueOf(charArray[i]));
                        int count = current * idCardWi[i];
                        sum += count;
                    }
                    char idCardLast = charArray[17];
                    int idCardMod = sum % 11;
                    if (idCardY[idCardMod].toUpperCase()
                            .equals(String.valueOf(idCardLast).toUpperCase())) {
                        return true;
                    } else {
                        System.out.println("身份证最后一位:" + String.valueOf(idCardLast).toUpperCase()
                                + "错误,正确的应该是:" + idCardY[idCardMod].toUpperCase());
                        return false;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }
            } else {
                return false;
            }
        }
        return matches;
    }

    /**
     * 将身份照解析出出生日期和性别
     * @return
     */
    public static IdcardVO analysis(String idCard){
        if (!isIDNumber(idCard)){
            return null;
        }

        String birthday = "";
        int gender = 1;
        int age = 0;
        if (idCard.length() == 18){
            //7~14位为出生年月日
            birthday = idCard.substring(6,10) + "-" + idCard.substring(10,12) + "-" + idCard.substring(12,14);
            age = LocalDate.now().getYear() - Integer.parseInt(idCard.substring(6,10));

            //15~17位为顺序号 单数为男性分配码，双数为女性分配码
            boolean flag = Integer.parseInt(idCard.substring(14,17)) %2 == 0;
            if (flag){
                gender = 2;
            }
        }

        IdcardVO idcardVO = new IdcardVO();
        idcardVO.setBiethday(birthday);
        idcardVO.setAge(age);
        idcardVO.setGender(gender);
        idcardVO.setIdCard(idCard);
        return idcardVO;
    }

    public static void main(String[] args) {
        //372301 19880918 2422
//        String idCard = "372301198809182422";
//        System.out.println(idCard.substring(6,14));
//        String birthday = idCard.substring(6,10) + "-" + idCard.substring(10,12) + "-" + idCard.substring(12,14);
//        System.out.println(birthday);
//
//        boolean flag = Integer.parseInt(idCard.substring(14,17)) %2 == 0;
//        System.out.println(idCard.substring(14,17));
//        System.out.println(flag);

        IdcardVO idcardVO =  IdcardUtils.analysis("370203199408015543");
        System.out.println(idcardVO);

    }

}
